package com.demo.controller;

import com.demo.entity.*;
import com.demo.repository.GradeRepository;
import com.demo.repository.TeacherRepository;
import com.demo.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    TeacherRepository teacherRepository;
    @Autowired
    GradeRepository gradeRepository;

    @RequestMapping("/teacherLogin")
    public String teacherLogin(HttpServletRequest request, String teacherName, String password) {
        Teacher teacher = teacherService.Login(teacherName, password);
        if (teacher == null) {
            return "账号或者密码错误";
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("teacher", teacher);
            return "home";
        }
    }
    //展示当前老师所教课程的所有学生
    @RequestMapping("/getAllGrade")
    @ResponseBody
    public PageInfo<StudentGrade> select(HttpServletRequest request, Model model,Integer pageNo, Integer pageSize){
        Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
        String subjectName = teacher.getSubjectName();
        PageHelper.startPage(pageNo, pageSize);
        List<StudentGrade> students = teacherService.teacherSelect(subjectName);
        PageInfo<StudentGrade> pageInfo = new PageInfo<>(students);
        model.addAttribute("tableData",students);
        return pageInfo;
    }


    //搜索框，展示当前老师教授课程的所有学生根据名字搜索
    @RequestMapping("/getGradeByFilter")
    @ResponseBody
    public PageInfo<StudentGrade> sousuo(HttpServletRequest request,Integer classId,Integer season,String keywords,Model model,Integer pageNo, Integer pageSize){
        Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
        String subjectName = teacher.getSubjectName();
        PageHelper.startPage(pageNo, pageSize);
        List<StudentGrade> students = teacherService.souSuo(classId,season,keywords,subjectName);
        PageInfo<StudentGrade> pageInfo = new PageInfo<>(students);
        model.addAttribute("tableData",students);
        return pageInfo;
    }
    //老师插入学生成绩
    @RequestMapping("/insertGrade")
    @ResponseBody
    public String insertGrade(Integer stuId,Integer grade){
        GradeExample gradeExample=new GradeExample();
        gradeExample.createCriteria().andStuIdEqualTo(stuId);
        //Grade grades = gradeRepository.selectByExample(gradeExample).get(0);
            Grade oldgrade =new Grade();
            oldgrade.setGrade(grade);
            gradeRepository.updateByExampleSelective(oldgrade,gradeExample);
        return "修改成功";

    }

    //老师修改账号密码
    @RequestMapping("/teacherUpdate")
    @ResponseBody
    public String teacherUpdate(HttpServletRequest request, @RequestBody Teacher teacher,@RequestParam String oldPassword){
        Teacher oldteacher = (Teacher) request.getSession().getAttribute("teacher");
        if(oldPassword.equals(oldteacher.getPassword())){
            TeacherExample teacherExample=new TeacherExample();
            teacherExample.createCriteria().andTeacherNameEqualTo(teacher.getTeacherName());
            List<Teacher> teachers = teacherRepository.selectByExample(teacherExample);
            if(!teachers.isEmpty()){
                return "用户名已经存在";
            }
            TeacherExample teacherExample1=new TeacherExample();
            teacherExample1.createCriteria().andIdEqualTo(teacher.getId());
            teacherRepository.updateByExampleSelective(teacher,teacherExample1);
            return "修改成功";
        }else {
            return "密码错误";
        }

    }

}
